package com.softer.chengxin.corp.controller;

import com.softer.chengxin.common.domin.Common;
import com.softer.chengxin.common.domin.CommonResult;
import com.softer.chengxin.common.domin.ManagerPageResult;
import com.softer.chengxin.common.domin.Page;
import com.softer.chengxin.common.enums.ResultEnum;
import com.softer.chengxin.common.util.DateFormatUtil;
import com.softer.chengxin.corp.domin.ApproveDTO;
import com.softer.chengxin.corp.domin.Corp;
import com.softer.chengxin.corp.service.CorpService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Gong Zhiyang
 * @date 2018/10/8
 */

@RestController
@RequestMapping("/api/WY_CorpBaseInfo")
public class CorpController {
    @Autowired
    private CorpService corpService;

    /**
     * 获取企业基本信息
     */
    @PostMapping("/load")
    public Corp load(HttpSession session) {
        //查询物业企业Id
        Integer id = (Integer) session.getAttribute("ID");
        if (id == null) {
            return new Corp();
        }
        return corpService.load(id);
    }

    /**
     * 提交审核
     *
     * @param session 缓存
     * @param state   状态
     * @return 处理结果
     */
    @PostMapping("/updateState")
    public CommonResult updateState(HttpSession session, Integer state) {
        //查询企业ID
        Integer id = (Integer) session.getAttribute("ID");
        if (id == null) {
            return new CommonResult(ResultEnum.FAIL.getCode(), "企业基本信息提交审核失败，请重新登陆。");
        }
        return corpService.updateState(id, state);
    }

    /**
     * 保存企业基本信息
     *
     * @param corp    企业信息
     * @param session 缓存
     * @return 处理结果
     */
    @PostMapping("/update")
    public CommonResult update(Corp corp, HttpSession session) {
        // 查询企业ID
        Integer id = (Integer) session.getAttribute("ID");
        if (id == null) {
            return new CommonResult(ResultEnum.FAIL.getCode(), "请重新登陆系统");
        }
        corp.setId(id);
        Integer street = corp.getStreet();
        if (street == null) {
            corp.setStreet(Integer.parseInt(Common.ZERO));
        }
        String businessTime = corp.getBusinessTime();
        String expirationBeginTime = corp.getExpirationBeginTime();
        String firstContractTime = corp.getFirstContractTime();
        String expirationEndTime = corp.getExpirationEndTime();
        String qualificationApprovalTime = corp.getQualificationApprovalTime();
        String qualificationExpirationTime = corp.getQualificationExpirationTime();
        try {
            if (StringUtils.isBlank(businessTime)) {
                corp.setBusinessDate(null);
                corp.setBusinessTime(null);
            }
            if (StringUtils.isBlank(expirationBeginTime)) {
                corp.setExpirationBegin(null);
                corp.setExpirationBeginTime(null);
            }
            if (StringUtils.isBlank(expirationEndTime)) {
                corp.setExpirationEnd(null);
                corp.setExpirationEndTime(null);
            }
            if (StringUtils.isBlank(firstContractTime)) {
                corp.setFirstContractDate(null);
                corp.setFirstContractTime(null);
            }
            if (StringUtils.isBlank(qualificationApprovalTime)) {
                corp.setQualificationApprovalDate(null);
                corp.setQualificationApprovalTime(null);
            }
            if (StringUtils.isBlank(qualificationExpirationTime)) {
                corp.setQualificationApprovalDate(null);
                corp.setQualificationApprovalTime(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return corpService.update(corp);
    }

    /**
     * 主管部门，查询企业信息审批待办事项
     *
     * @param page    页码
     * @param state   状态
     * @param name    企业名称
     * @param session 缓存
     * @return 结果集
     */
    @PostMapping(value = "/qysb")
    public ManagerPageResult qysb(Page page, String state, String name, HttpSession session) {
        String loginName = (String) session.getAttribute("loginName");
        if (StringUtils.isBlank(loginName)) {
            return new ManagerPageResult();
        }
        return corpService.qysb(page, state, loginName, name);
    }

    /**
     * 管理员审批企业信息
     *
     * @param approveDTO 审批意见实体
     * @return 处理结果
     */
    @PostMapping("/sp")
    public CommonResult sp(ApproveDTO approveDTO, HttpSession session) {
        String loginName = (String) session.getAttribute("loginName");
        if (StringUtils.isBlank(loginName)){
            return new CommonResult(ResultEnum.FAIL.getCode(),"请重新登陆后进行操作");
        }
        return corpService.sp(approveDTO, loginName);
    }

    /**
     * 信息提交页面
     *
     * @param session 缓存
     * @return 信息
     */
    @PostMapping("/querySubmitMsg")
    public ManagerPageResult querySubmitMsg(HttpSession session) {
        //查询企业ID
        Integer id = (Integer) session.getAttribute("ID");
        if (id == null) {
            return new ManagerPageResult();
        }
        return corpService.querySubmitMsg(id);
    }

    /**
     * 查询企业当前状态
     *
     * @param session
     * @return
     */
    @PostMapping("/queryState")
    public Integer queryState(HttpSession session) {
        String loginName = (String) session.getAttribute("loginName");
        return corpService.queryState(loginName);
    }
}
